package com.dj.shop.util;

import com.dj.shop.vo.Result;
import com.qingstor.sdk.config.EnvContext;
import com.qingstor.sdk.constants.QSConstant;
import com.qingstor.sdk.exception.QSException;
import com.qingstor.sdk.service.Bucket;
import com.qingstor.sdk.service.QingStor;
import com.qingstor.sdk.service.Types;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Date;
import java.util.List;

/**
 * @author: create by yyj
 * @version: v1.0
 * @description: com.dj.shop.util
 * @date:2019/3/9
 */

public class QingStorUtil {

    private final Logger logger = LoggerFactory.getLogger(QingStorUtil.class);

    private static final String ACCESS_KEY = "CMWZCVXHLEMRNCPAHBPI";
    private static final String ACCESS_SECRET = "vNki1YOWhRlCgUbFdOMUqJXRME19xxoOHb6gVKFF";

    public static final String PROTOCOL = "https";
    public static final String HOST = "qingstor.com";
    public static final String prefix = "https://pek3b.qingstor.com/djshop";

    //创建bucket对象
    public static Bucket CreatBucket(){
        QingStorUtil qingStorUtil = new QingStorUtil();
        EnvContext context = new EnvContext(ACCESS_KEY, ACCESS_SECRET);
        context.setProtocol(PROTOCOL);
        context.setHost(HOST);
        context.setRequestUrlStyle(QSConstant.PATH_STYLE);
        String zoneKey = "pek3b";
        String bucketName = "djshop";
        Bucket bucket = new Bucket(context, zoneKey, bucketName);
        return bucket;
    }




//    public static void main(String... args) throws Exception {
//        QingStorUtil qingStorUtil = new QingStorUtil();
//        EnvContext context = new EnvContext(ACCESS_KEY, ACCESS_SECRET);
//        context.setProtocol(PROTOCOL);
//        context.setHost(HOST);
//        context.setRequestUrlStyle(QSConstant.PATH_STYLE);
//        String zoneKey = "pek3b";
//        //上传或下载文件的bucket名称
//        String bucketName = "djshop";
//        //上传或下载的文件路径
//        String objectKey = "qu.png";
//        //上传文件的本地路径
//        String filePath = "D:\\qq\\qu.png";
//        //下载文件的本地路径
//        String localKeptPath = "D:\\qu.png";
//        Bucket bucket = new Bucket(context, zoneKey, bucketName);
////        qingStorUtil.putObject(bucket,objectKey,filePath);
//       // qingStorUtil.getUrl(bucket);
//        qingStorUtil.putObject(bucket,objectKey,filePath);
//    }

    //上传对象
    public static Result putObject(Bucket bucket, String objectKey, String filePath){
        File file = new File(filePath);
        if (!file.exists() || file.isDirectory())
            System.out.println("文件不存在或是一个目录");
        Bucket.PutObjectInput input = new Bucket.PutObjectInput();
        input.setBodyInputFile(file);
        try {
            Bucket.PutObjectOutput output = bucket.putObject(objectKey, input);
            if (output.getStatueCode() == 201) {
                return Result.success("上传成功:文件路径为"+objectKey);
            } else {
                // Failed
                System.out.println("Failed to PUT object.");
                System.out.println("key = " + objectKey);
                System.out.println("StatueCode = " + output.getStatueCode());
                System.out.println("Message = " + output.getMessage());
                System.out.println("RequestId = " + output.getRequestId());
                System.out.println("Code = " + output.getCode());
                System.out.println("Url = " + output.getUrl());

            }
        } catch (QSException e) {
            e.printStackTrace();
        }
        return Result.error("上传失败");
    }

    //返回下载链接
    public static String getUrl(String Filename){
        String ObjUrl = prefix+"/"+Filename;
        return ObjUrl;
    }

    //下载对象
//    public void getObject(Bucket bucket, String objectKey, String localKeptPath) {
//        try {
//            Bucket.GetObjectInput input = new Bucket.GetObjectInput();
//            Bucket.GetObjectOutput output = bucket.getObject(objectKey, input);
//            InputStream inputStream = output.getBodyInputStream();
//            if (output.getStatueCode() == 200) {
//                if (inputStream != null) {
//
//                    FileOutputStream fos = new FileOutputStream(localKeptPath);
//                    int len;
//                    byte[] bytes = new byte[4096];
//                    while ((len = inputStream.read(bytes)) != -1) {
//                        fos.write(bytes, 0, len);
//                    }
//                    fos.flush();
//                    fos.close();
//                    System.out.println("Get object success.");
//                    System.out.println("ObjectKey = " + objectKey);
//                    System.out.println("LocalKeptPath = " + localKeptPath);
//                } else {
//                    System.out.println("Get object status code == 200, but inputStream is null, skipped.");
//                }
//            } else {
//                // Failed
//                System.out.println("Failed to get object.");
//                System.out.println("StatueCode = " + output.getStatueCode());
//                System.out.println("Message = " + output.getMessage());
//                System.out.println("RequestId = " + output.getRequestId());
//                System.out.println("Code = " + output.getCode());
//                System.out.println("Url = " + output.getUrl());
//            }
//            if (inputStream != null) inputStream.close();
//        } catch (QSException | IOException e) {
//            e.printStackTrace();
//        }
//    }


    //创建bucket存储空间
    public void creatBucket(Bucket bucket){
        try {
            Bucket.PutBucketOutput output = bucket.put();
            if (output.getStatueCode() == 201) {
                // Created
                System.out.println("Put Bucket: Created.");
            } else {
                // Failed
                System.out.println("Failed to Put Bucket.");
                System.out.println("StatueCode = " + output.getStatueCode());
                System.out.println("Message = " + output.getMessage());
                System.out.println("RequestId = " + output.getRequestId());
                System.out.println("Code = " + output.getCode());
                System.out.println("Url = " + output.getUrl());
            }

        } catch (QSException e) {
            e.printStackTrace();
        }
    }

}
